<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.importance" placeholder="Imp" clearable style="width: 90px" class="filter-item">
        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="listQuery.type" placeholder="Type" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button> -->
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row>
      <el-table-column align="center" label="ID" min-width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数据源">
        <template slot-scope="{row}">
          <span>{{ row.driver_name+': '+row.the_table+'.'+row.the_field }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="120px" align="center" label="黑名单名称">
        <template slot-scope="scope">
          <span>{{ scope.row.black_list_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="路径" prop="black_list_path" />
      <el-table-column label="错误码" prop="output_code" />

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="黑名单" :visible.sync="dialogVisible">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数据源" prop="data_source_id">
          <el-select v-model="form.data_source_id" autocomplete="off" style="width:100%">
            <el-option v-for="item in dataList" :key="item.id" :value="item.id" :label="item.id+'. '+item.driver_name+': '+item.the_table+'.'+item.the_field" />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="black_list_name">
          <el-input v-model="form.black_list_name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="路径" prop="black_list_path">
          <el-input v-model="form.black_list_path" autocomplete="off" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.black_list_description" type="textarea" />
        </el-form-item>
        <el-form-item label="错误码" prop="output_code">
          <el-input v-model.number="form.output_code" />
        </el-form-item>
        <el-form-item label="错误信息" prop="output_message">
          <el-input v-model="form.output_message" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, create } from '@/api/blacklist'
import { getList as dataSList } from '@/api/blackdatasource'
import Pagination from '@/components/Pagination'

const defaultForm = {
  data_source_id: '',
  black_list_name: '',
  black_list_path: '',
  black_list_description: '',
  output_code: '',
  output_message: ''
}

export default {
  name: 'BlackList',
  components: { Pagination },
  data() {
    return {
      list: null,
      dataList: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      form: defaultForm,
      rules: {
        data_source_id: [{ required: true, message: '请选择数据源', trigger: 'blur' }],
        black_list_name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        black_list_path: [{ required: true, message: '请输入路径', trigger: 'blur' }],
        black_list_description: [{ required: true, message: '请输入描述', trigger: 'blur' }],
        output_code: [
          { required: true, message: '请输入返回错误码', trigger: 'blur' },
          { type: 'number', message: 'code必须为数字值' }
        ],
        output_message: [{ required: true, message: '请输入错误信息', trigger: 'blur' }]
      },
      dialogVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    dataSourceList() {
      this.listLoading = true
      dataSList({ page: 1, limit: 1000 }).then(response => {
        this.dataList = response.list
      })
    },
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.list
        this.total = response.total
        this.listLoading = false
      })
    },
    handleCreate() {
      this.dataSourceList()
      this.form = Object.assign({}, defaultForm)
      this.dialogVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          create(this.form).then(rep => {
            this.$message({
              message: '数据保存成功',
              type: 'success'
            })
            this.getList()
            this.dialogVisible = false
          })
        } else {
          this.$message.error('提交失败，请检查提交数据')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
